# node tools/config.py

import string
import sys

arch = "linux"
nodeid = "Test"
plugins = []
bplugins = []
debug = True
maxbuf = {"loadavg" : 5}
alert_min = {"uptime" : 1, "loadavg" : 0}
alert_max = {"uptime" : 11111, "loadavg" : 5}
net = "tcp"
tcp_port = 11111
irc_port = 0
irc_server = ""
alert_check = 60
#collect_int = 60
collect_int = {}

def init () :
	global nodeid
	global plugins
	global bplugins
	global debug
	global maxbuf
	global net
	global arch
	global alert_min
	global alert_max
	global alert_check
	global collect_int
	global irc_server
	global irc_port
	global tcp_port
	
	try :
		f = open ("etc/pyhmon_node.conf")
		cont = list (string.split (f.read (), "\n"))
		f.close ()
	except :
		sys.exit("Cannot load configfile: etc/pyhmon_node.conf")
	for c in cont :
		if len (c) > 0 :
			if not c[0] == "#" :
				param = list (string.split (c, "="))
				if param[0] == "arch" :
					arch = param[1]
				elif param[0] == "plugins" :
					plugins = list (string.split (param[1], ","))
				elif param[0] == "net" :
					net = param[1]
				elif param[0] == "id" :
					nodeid = param[1]
				elif param[0] == "alert_check" :
					alert_check = param[1]
				elif param[0] == "debug" :
					if param[1] == "no" :
						debug = False
					if param[1] == "yes" :
						debug = True

	try :
		f = open ("etc/net/%s.conf" % net)
		cont = list (string.split (f.read (), "\n"))
		f.close ()
	except :
		sys.exit("Cannot load configfile: etc/net/%s.conf" % net)
	
	for c in cont :
		if len(c) > 0 :
			if not c[0] == "#" :
				param = list (string.split (c, "="))
				if param[0] == "port" :
					if net == "irc" :
						irc_port = param[1]
					elif net == "tcp" :
						tcp_port = param[1]
				elif param[0] == "server" :
					if net == "irc" :
						irc_server = param[1]
			
	for plugin in plugins :
		try :
			f = open ("etc/plugins/%s.conf" % plugin)
			cont = list (string.split (f.read(), "\n"))
			f.close ()
		except :
			sys.exit("Cannot load configfile: etc/plugins/%s.conf" % plugin)

		for c in cont :
			if len (c) > 0 :
				if not c[0] == "#" :
					param = list (string.split (c, "="))
					if param[0] == "buffersize" :
						if not int(param[1]) == 0 :
							maxbuf[plugin] = param[1]
							bplugins.append (plugin)
					if param[0] == "alert_min" :
						alert_min[plugin] = param[1]
					if param[0] == "alert_max" :
						alert_max[plugin] = param[1]
					if param[0] == "collect_interval" :
						collect_int[plugin] = int (param[1])

def getNodeID () :
	global node
	return nodeid

def getArch () :
	global arch
	return arch

def getMaxBuf (pluginname) :
	global maxbuf
	return int (maxbuf[pluginname])

def getPluginsToBuffer () :
	global bplugins
	return bplugins

def getAllPlugins () :
	global plugins
	return plugins

def getAlertMin (pluginname) :
	global alert_min
	return int (alert_min[pluginname])

def getAlertMax (pluginname) :
	global alert_max
	return int (alert_max[pluginname])

def getTCPPort () :
	global tcp_port
	return int (tcp_port)

def getAlertCheckTime () :
	global alert_check
	return int (alert_check)

def getCollectInterval (pluginname) :
	global collect_int
	return collect_int[pluginname]

def getIRCPort () :
	global irc_port
	return int (irc_port)

def getIRCServer () :
	global irc_server
	return irc_server

def getNetBackend () :
	global net
	return net

def getDebug () :
	global debug
	return debug
